
'use strict';
const express = require('express');
const router = express.Router();

/* GET home page. */
router.use('*', function(req, res, next) {
    console.log("come in index.js");
    next();
    /*if(code){
        wechatUtil.getOpenidByCode(code)
            .then(function (e) {
                opid = e.openid;
                req.viewParam.openid = opid;
                request({
                    method: 'get',
                    uri: `${domain}/LenLeWebWeixinNew/WeixinService.asmx/getVipInfoByWxid?wxid=${opid}`,
                    json: true
                }, function(e, res, data){
                    let jsonStr = xml2json.parse(data);
                    let json = JSON.parse(jsonStr.root.content);
                    if( json.ResultCode=='1'){
                        res.redirect(req.viewParam.linkPath+'/login');
                    }else{
                        req.viewParam.userData = json.ResultValue[0].errMsg.List[0];
                        userCard =  req.viewParam.userData.cardCode;
                        next();
                    }
                });
            },function (e) {
                logger.info(e)
            });
    }else{
        request({
            method: 'get',
            uri: `${domain}/LenLeWebWeixinNew/WeixinService.asmx/getVipInfoByWxid?wxid=${opid}`,
            json: true
        }, function(e, res, data){
            let jsonStr = xml2json.parse(data);
            let json = JSON.parse(jsonStr.root.content);
            req.viewParam.userData = json.ResultValue[0].errMsg.List[0];
            userCard =  req.viewParam.userData.cardCode;
            next();
        });
    }*/
});
/*router.use('*', function(req, res, next) {
    console.log("come in 1_index.js");
    console.log('code-----------------');
    console.log(req.query.code);
    console.log('url----------------' + req.originalUrl);
    console.log('url----------------' + req.url);
    console.log(next);

    let getUserByOpid = function (opid) {
        request({
            method: 'get',
            uri: `${domain}/LenLeWebWeixinNew/WeixinService.asmx/getVipInfoByWxid?wxid=${opid}`,
            json: true
        }, function(e, response, data){
            let jsonStr = xml2json.parse(data);
            let json = JSON.parse(jsonStr.root.content);
            console.log(json);
            if( json.ResultCode=='1'){
                logger.info(4444444444444444444444)
                /!*res.redirect(req.viewParam.linkPath+'/login');*!/
            }else{
                /!*req.session.userData*!/
                logger.info(555555555555555)
                req.viewParam.userData = json.ResultValue[0].errMsg.List[0];
                req.viewParam.cardCode =  req.viewParam.userData.cardCode;
                return next();
            }
        });
    }
    let language, loginFlag, dataFlag,appid = req.query.gId,wechatUtil;
    req.viewParam = {};
    let theme=req.query.brand,type;
    if(req.query.orgCode){
        req.viewParam.orgCode= req.query.orgCode;
    }
    switch (theme){
        case 'lenle':
            type = global.systemConstant.path.lenle;
            break;
        case 'tgp':
            type = global.systemConstant.path.tgp;
            break;
        case 'lno':
            type = global.systemConstant.path.lno;
            break;
        case 'Mwood':
            type = global.systemConstant.path.Mwood;
            break;
        case 'upMark':
            type = global.systemConstant.path.upMark;
            break;
        case 'ira':
            type = global.systemConstant.path.ira;
            break;
        default :
            type = global.systemConstant.path.tgp;
    }
    req.viewParam.imgPath =  type.imgPath;
    req.viewParam.stylePath = type.style;
    req.viewParam.linkPath = type.link;

    language = req.headers["accept-language"] || 'zh-CN';
    req.setLocale(language.substr(0,2));

    // req.endType = "";
    req.endType = global.util.endType(req.headers['user-agent']);

    req.defaultParam = {
        path : null,
        redirect : null,
        data : req.query || {},
        body : {},
        userData:req.session.userData,
        cookie : req.cookies
    };

    if(appid){

        console.log('appid------------'+appid);
        wechatUtil = global.cache.officialAccount[appid];
    }else{
        wechatUtil = global.cache.officialAccount['f06ec5fa-c295-46e3-9887-660245900ee8'];
    }

    //check login
    loginFlag = Boolean(req.session.userData);


    if(loginFlag){
        console.log('11111111111111111111111');
        logger.info(11111111111111111111111)
        req.curUser = req.session.userData;
        next();
    }else{
        //auto login
        if(req.query.openid){
            console.log('11111111111111111111111');
            logger.info(222222222222222)
            let opid = req.query.openid;
            console.log('--------opid' + opid);
            getUserByOpid(opid);
            /!*       wechatUtil.getCustomerInfo(openid).then(data => {
               req.session.userData = data;
               next();
           }, err => {
               res.redirect(global.config.path.contextPath+'/login?redirect='+encodeURIComponent(req.baseUrl));
           });*!/
        }else if(req.query.code){
            logger.info(333333333333333333333)
            console.log('code-------------' + req.query.code);
            wechatUtil.getOpenidByCode(req.query.code)
                .then(function (e) {
                    var promise =new Promise(function (resolve,reject) {
                        let opid = e;
                        req.viewParam.openid = opid;
                        request({
                            method: 'get',
                            uri: `${domain}/LenLeWebWeixinNew/WeixinService.asmx/getVipInfoByWxid?wxid=${opid}`,
                            json: true
                        }, function(e, response, data){
                            let jsonStr = xml2json.parse(data);
                            let json = JSON.parse(jsonStr.root.content);
                            console.log(json);
                            if( json.ResultCode=='1'){
                                logger.info(4444444444444444444444)
                                /!*  res.redirect(req.viewParam.linkPath+'/login');*!/
                                resolve(1);
                            }else{
                                /!*req.session.userData*!/
                                logger.info(6666666666666666)
                                req.viewParam.userData = json.ResultValue[0].errMsg.List[0];
                                req.viewParam.cardCode =  req.viewParam.userData.cardCode;
                                console.log(77777777777777777);
                                resolve(1);
                            }
                        });
                    });
                    promise.then(function (e) {
                        console.log(e);
                        console.log('--------------------');
                        next()
                    },function () {
                    })

                },function (e) {
                    logger.info(e)
                });
        }
        dataFlag = req.headers["ajax-flag"];
        if(dataFlag){
            res.send(resUtil.forbidden());
        }else{
            /!*  res.redirect(req.viewParam.linkPath+'/login');*!/
        }
    }
});*/


module.exports = router;
